package com.dubbo;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caoqianhuan
 * @date 2022/9/27
 * @Description: 弱引用 虚拟机参数 -Xmx20m(调整堆内存为20m)  -XXPrintGCDetails -verbose:gc(显示gc详细信息)
 */
public class WeakReferenceExample {
    public static final int _4MB = 4 * 1024 * 1024;
    public static void main(String[] args) {
        List<WeakReference<byte[]>> images = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            WeakReference<byte[]> bytesWeakReference = new WeakReference<>(new byte[_4MB]);
            images.add(bytesWeakReference);
            System.out.println(images.get(i).get());
        }
    }
}

/**
 * [B@4554617c
 * [B@74a14482
 * [B@1540e19d
 * [GC (Allocation Failure) [PSYoungGen: 1893K->488K(6144K)] 14181K->12992K(19968K), 0.0010246 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [B@677327b6
 * [GC (Allocation Failure) [PSYoungGen: 4696K->496K(6144K)] 17200K->13048K(19968K), 0.0007289 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
 * [B@14ae5a5
 * Heap
 *  PSYoungGen      total 6144K, used 4761K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
 *   eden space 5632K, 75% used [0x00000000ff980000,0x00000000ffdaa498,0x00000000fff00000)
 *   from space 512K, 96% used [0x00000000fff80000,0x00000000ffffc040,0x0000000100000000)
 *   to   space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
 *  ParOldGen       total 13824K, used 12552K [0x00000000fec00000, 0x00000000ff980000, 0x00000000ff980000)
 *   object space 13824K, 90% used [0x00000000fec00000,0x00000000ff842030,0x00000000ff980000)
 *  Metaspace       used 3246K, capacity 4496K, committed 4864K, reserved 1056768K
 *   class space    used 352K, capacity 388K, committed 512K, reserved 1048576K
 *
 * Process finished with exit code 0
 */

